import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from Utils import ShoppingDriver
from selenium.webdriver.support import expected_conditions as EC

class ShoppingHome:
  url = ""
  driver = ""

  def __init__(self):
        self.driver = ShoppingDriver.driver
        self.url = "http://8.155.1.153/"
        self.driver.get(self.url)
        self.driver.fullscreen_window()

  def Switch_windows(self,state): #窗口切换管理
     if state == True:
         #出现了新窗口，跳转到新窗口
        curwindow = self.driver.current_window_handle   #获取当前窗口的句柄
        allwindow = self.driver.window_handles          #获取所有窗口句柄
        for window in allwindow:
            if window != curwindow:
                self.driver.switch_to.window(window)    #切换到新窗口
                break

     else:
         #关闭窗口，回到原窗口
         self.driver.close()
         allwindow = self.driver.window_handles         #获得所有窗口句柄
         for window in allwindow:
             if window:
                 self.driver.switch_to.window(window)
                 break;
             else:
                 print("异常，所有窗口已经关闭")
                 ShoppingDriver.getScreenShot()         #截图记录

#未登录状态首页功能验证
def ShoppingHomeNotLogin(self):
    try:
        # 未登录状态下，验证和登录状态下不同的地方：无法显示个人信息，无法查看导航区，无法对商品进行操作（收藏，购买等）
        url = self.driver.current_url
        if url != "http://8.155.1.153/":
            print("首页网址错误")
        # am-modal-cytgb > div > span
        # 导航区点击个人中心、我的收藏—商品收藏、购物车，均弹出登录提示
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-center > div > a > span").click()
        # 关闭按钮css值变化，无法直接定位，需要先定位到特定弹窗的大容器中
        popup = self.driver.find_element(By.CSS_SELECTOR,
                                         "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active")
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active"))
        )  # 关闭登录窗口的按钮加载好，可以点击时
        popup.find_element(By.CSS_SELECTOR, "span[data-am-modal-close].am-close.am-close-alt").click()
        # 点击我的收藏和商品收藏
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > a > span").click()
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > ul > li > a"))
        )  # 按钮是否可点击
        element.click()  # 点击商品收藏按钮，跳转到收藏夹页面
        # 关闭按钮css值变化，无法直接定位，需要先定位到特定弹窗的大容器中
        popup = self.driver.find_element(By.CSS_SELECTOR,
                                         "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active")
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active"))
        )  # 关闭登录窗口的按钮加载好，可以点击时
        popup.find_element(By.CSS_SELECTOR, "span[data-am-modal-close].am-close.am-close-alt").click()
        # 点击购物车
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > span").click()
        # 关闭按钮css值变化，无法直接定位，需要先定位到特定弹窗的大容器中
        popup = self.driver.find_element(By.CSS_SELECTOR,
                                         "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active")
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active"))
        )  # 关闭登录窗口的按钮加载好，可以点击时
        popup.find_element(By.CSS_SELECTOR, "span[data-am-modal-close].am-close.am-close-alt").click()
        # 模拟鼠标向下滑动,到数码办公区
        # 向下滚动650像素
        self.driver.execute_script("window.scrollBy(0, 650);")
        # 进入商品详情页
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child(1) > div > a > img").click()
        # 窗口句柄跳转
        ShoppingHome.Switch_windows(self, True)
        # 点击收藏
        self.driver.find_element(By.CSS_SELECTOR, "#clearcontent > div.photo-bottom.am-cf > span > em").click()
        # 关闭按钮css值变化，无法直接定位，需要先定位到特定弹窗的大容器中
        popup = self.driver.find_element(By.CSS_SELECTOR,
                                         "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active")
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active"))
        )  # 关闭登录窗口的按钮加载好，可以点击时
        popup.find_element(By.CSS_SELECTOR, "span[data-am-modal-close].am-close.am-close-alt").click()
        # 点击加入购物车
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-secondary.cart-submit.buy-event.login-event").click()
        # 关闭按钮css值变化，无法直接定位，需要先定位到特定弹窗的大容器中
        popup = self.driver.find_element(By.CSS_SELECTOR,
                                         "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active")
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active"))
        )  # 关闭登录窗口的按钮加载好，可以点击时
        popup.find_element(By.CSS_SELECTOR, "span[data-am-modal-close].am-close.am-close-alt").click()
        # 点击购买
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-primary.buy-submit.buy-event.login-event").click()
        # 关闭按钮css值变化，无法直接定位，需要先定位到特定弹窗的大容器中
        popup = self.driver.find_element(By.CSS_SELECTOR,
                                         "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active")
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "div.am-popup.am-radius.popup-iframe.common-login-modal.popup-not-title.am-modal-active"))
        )  # 关闭登录窗口的按钮加载好，可以点击时
        popup.find_element(By.CSS_SELECTOR, "span[data-am-modal-close].am-close.am-close-alt").click()

        # 关闭新窗口，回到主窗口
        ShoppingHome.Switch_windows(self, False)
    except Exception as e:
        print("ShoppingHomeNotLogin failed")
        print(e)
        # 添加截图
        ShoppingDriver.getScreeShot()
        raise


#登录后首页元素完整性验证
def ShoppingHomeCheckByLogin(self):
    # 登录状态下，页面元素检查
    try:
    #顶部导航区
        time.sleep(1)
        #个人中心
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-center > div > a > span")
        # 1. 检查元素是否可见：is_displayed()
        assert element.is_displayed()
        # 2. 检查元素是否可点击（简单版，更严谨建议用显式等待的 element_to_be_clickable）
        # 这里 is_enabled 主要针对 disabled 属性，若元素本身不可点击（比如按钮 disabled）会返回 False
        assert element.is_enabled()
        # 我的商城
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-center > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
        # 我的收藏
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
        # 更多入口
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-quick > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
        # 购物车
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
        # 消息
        element = self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-message > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
    #商品全部分类区
        # 数码办公
        element = self.driver.find_element(By.CSS_SELECTOR,"#goods-category > div > div > div > ul > li:nth-child(1) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
        # 时尚服饰
        element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(2) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
        # 品名潮包
        element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(3) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
        # 个护化妆
        element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(4) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
        # 珠宝手表
        element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(5) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
        # 运动健康
        element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(6) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
        # 汽车用品
        element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(7) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
        # 玩具乐器
        element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(8) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
        # 母婴用品
        element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(9) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
        # 生活服务
        element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(10) > a > div > h3")
        assert element.is_displayed()
        assert element.is_enabled()
    # 个人信息栏
        # 头像
        element = self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.m-baseinfo.am-flex.am-flex-items-center > a > img")
        assert element.is_displayed()
        assert element.is_enabled()
        # 待付款
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.member-center > a:nth-child(1)")
        assert element.is_displayed()
        assert element.is_enabled()
        # 待发货
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.member-center > a:nth-child(2)")
        assert element.is_displayed()
        assert element.is_enabled()
        # 待收货
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.member-center > a:nth-child(3)")
        assert element.is_displayed()
        assert element.is_enabled()
        # 待评价
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.member-center > a:nth-child(4)")
        assert element.is_displayed()
        assert element.is_enabled()
        # 关于我们
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(1) > a")
        assert element.is_displayed()
        assert element.is_enabled()
        # 联系我们
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(2) > a")
        assert element.is_displayed()
        assert element.is_enabled()
        # 招聘英才
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(3) > a")
        assert element.is_displayed()
        assert element.is_enabled()
        # 合作及洽谈
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(4) > a")
        assert element.is_displayed()
        assert element.is_enabled()
        # 修改收货地址
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(5) > a")
        assert element.is_displayed()
        assert element.is_enabled()
        # 商品发布
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(6) > a")
        assert element.is_displayed()
        assert element.is_enabled()
        # 会员修改个人资料
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(7) > a")
        assert element.is_displayed()
        assert element.is_enabled()
        # 会员修改密码
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(8) > a")
        assert element.is_displayed()
        assert element.is_enabled()
        # 退款申请
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(9) > a")
        assert element.is_displayed()
        assert element.is_enabled()
    # 搜索栏
        # 搜索框
        element = self.driver.find_element(By.CSS_SELECTOR,"#search-input")
        assert element.is_displayed()
        assert element.is_enabled()
        # 搜索按钮
        element = self.driver.find_element(By.CSS_SELECTOR, "#ai-topsearch")
        assert element.is_displayed()
        assert element.is_enabled()
    # 首页图片轮换
        # 点击轮换人物界面
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.banner.theme-data-edit-event > div > ol > li:nth-child(1) > a").click()
        time.sleep(1)
        # 这时候再查找元素
        img=self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.banner.theme-data-edit-event > div > div > ul > li.am-active-slide > a > img")
        if not img.is_displayed():
            ShoppingDriver.getScreeShot()
            print("人物不可见")
        if not img.is_enabled():
            print("人物不可点击")

        # 点击轮换手机界面
        self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.banner.theme-data-edit-event > div > ol > li:nth-child(2) > a").click()
        time.sleep(1)
        # 这时候再查找元素
        img=self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.banner.theme-data-edit-event > div > div > ul > li.am-active-slide > a > img")
        if not img.is_displayed():
            ShoppingDriver.getScreeShot()
            print("手机不可见")
        if not img.is_enabled():
            print("手机不可点击")
    # 模拟鼠标向下滑动,到数码办公区
        # 向下滚动650像素
        self.driver.execute_script("window.scrollBy(0, 650);")
        #对数码办公区元素进行检查
        # 左示例图
        element = self.driver.find_element(By.CSS_SELECTOR, "#floor1 > div.am-g.am-container.floor.floor-flex > div.am-u-sm-3.aggregation-container > div > a > img")
        assert element.is_displayed()
        assert element.is_enabled()
        # 循环获取第 1 到第 8 个元素
        for i in range(1, 9):  # range(1,9) 表示 8张图
            # 用 f-string 格式化字符串，将变量 i 嵌入选择器
            selector_now = f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > strong"
            selector_prev = f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > span.original-price.am-text-truncate"

            element_now = self.driver.find_element(By.CSS_SELECTOR, selector_now)
            assert (element_now.is_displayed())
            text = element_now.text
            # print(f"第 {i} 张图的当前文本：", text)  # 打印元素文本
            price_now = float(text.replace("￥", "").replace("¥", ""))  # 去除货币符号
            assert (price_now > 0)  # 保证价格大于0

            element_prev = self.driver.find_elements(By.CSS_SELECTOR, selector_prev)
            # 一个商品可能是不打折的，所以element_prev可能不存在
            flag = True
            if len(element_prev) > 0:
                # 找到了元素，进行相应操作
                element = element_prev[0]
            else:
                flag = False
                element = element_now
            text = element.text
            # print(f"第 {i} 张图的折前文本：", text)  # 打印元素文本
            price_prev = float(text.replace("￥", "").replace("¥", ""))  # 去除货币符号
            assert (price_prev > 0)  # 保证价格大于0

            if ((price_prev <= price_now) and (flag == True)):  # 保证现在的价格比之前的价格便宜
                print(f"时尚服饰区第{i}张图价格标注错误")

        time.sleep(1)
        # 向下滚动700像素
        self.driver.execute_script("window.scrollBy(0, 700);")
        # 对时尚服饰区元素进行检查
        # 左示例图
        element = self.driver.find_element(By.CSS_SELECTOR, "#floor2 > div.am-g.am-container.floor.floor-flex > div.am-u-sm-3.aggregation-container > div > a > img")
        assert element.is_displayed()
        assert element.is_enabled()
        # 循环获取第 1 到第 8 个元素
        for i in range(1, 9):  # range(1,9) 表示 8张图
            # 用 f-string 格式化字符串，将变量 i 嵌入选择器
            selector_now = f"#floor2 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > strong"
            selector_prev = f"#floor2 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > span.original-price.am-text-truncate"

            element_now = self.driver.find_element(By.CSS_SELECTOR, selector_now)
            assert (element_now.is_displayed())
            text = element_now.text
            #print(f"第 {i} 张图的当前文本：", text)  # 打印元素文本
            price_now = float(text.replace("￥", "").replace("¥", ""))  # 去除货币符号
            assert (price_now > 0)  # 保证价格大于0

            element_prev = self.driver.find_elements(By.CSS_SELECTOR, selector_prev)
            # 一个商品可能是不打折的，所以element_prev可能不存在
            flag=True
            if len(element_prev) > 0:
                # 找到了元素，进行相应操作
                element = element_prev[0]
            else:
                flag=False
                element = element_now
            text = element.text
            #print(f"第 {i} 张图的折前文本：", text)  # 打印元素文本
            price_prev = float(text.replace("￥", "").replace("¥", ""))  # 去除货币符号
            assert (price_prev > 0)  # 保证价格大于0

            if ((price_prev <= price_now) and (flag==True)) : # 保证现在的价格比之前的价格便宜
                print(f"时尚服饰区第{i}张图价格标注错误")

        time.sleep(1)
        # 向下滚动500像素
        self.driver.execute_script("window.scrollBy(0, 670);")

        # 对名品潮包区元素进行检查
        # 左示例图
        element = self.driver.find_element(By.CSS_SELECTOR, "#floor3 > div.am-g.am-container.floor.floor-flex > div.am-u-sm-3.aggregation-container > div > a > img")
        assert element.is_displayed()
        assert element.is_enabled()
        # 循环获取第 1 到第 8 个元素
        for i in range(1, 9):  # range(1,9) 表示 8张图
            # 用 f-string 格式化字符串，将变量 i 嵌入选择器
            selector_now = f"#floor3 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > strong"
            selector_prev = f"#floor3 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > span.original-price.am-text-truncate"

            element_now = self.driver.find_element(By.CSS_SELECTOR, selector_now)
            assert (element_now.is_displayed())
            text = element_now.text
            # print(f"第 {i} 张图的当前文本：", text)  # 打印元素文本
            price_now = float(text.replace("￥", "").replace("¥", ""))  # 去除货币符号
            assert (price_now > 0)  # 保证价格大于0

            element_prev = self.driver.find_elements(By.CSS_SELECTOR, selector_prev)
            # 一个商品可能是不打折的，所以element_prev可能不存在
            flag = True
            if len(element_prev) > 0:
                # 找到了元素，进行相应操作
                element = element_prev[0]
            else:
                flag = False
                element = element_now
            text = element.text
            # print(f"第 {i} 张图的折前文本：", text)  # 打印元素文本
            price_prev = float(text.replace("￥", "").replace("¥", ""))  # 去除货币符号
            assert (price_prev > 0)  # 保证价格大于0

            if ((price_prev <= price_now) and (flag == True)):  # 保证现在的价格比之前的价格便宜
                print(f"时尚服饰区第{i}张图价格标注错误")

        self.driver.execute_script("window.scrollBy(0, -(670+700+650));")
    except Exception as e:
        print("ShoppingHomeCheckByLogin failed")
        print(e)
        # 添加截图
        ShoppingDriver.getScreeShot()
        raise

# 登录后个人信息管理功能测试
def Handle_person(self):
    #处理首页个人信息的交互
    number = 0
    time.sleep(2)
    try:
        # 个人中心
        element = self.driver.find_element(By.CSS_SELECTOR,
        "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-center > div > a > span")
        element.click()
        # 跳转后验证个人中心有个人头像、名字，并且可以修改头像、修改名字
        url = self.driver.current_url
        assert (url == "http://8.155.1.153/?s=user/index.html")

        #头像
        element = self.driver.find_element(By.CSS_SELECTOR,
        "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main.user-center > div.user-content > div > div.user-base.am-radius > div.user-base-left > div.user-avatar > img")
        assert element.is_displayed()
        #名字
        element = self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main.user-center > div.user-content > div > div.user-base.am-radius > div.user-base-left > div.user-avatar > div > span")
        assert element.is_displayed()
        assert  element.text=="dz000"
        time.sleep(2)
        #修改头像
        self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main.user-center > div.user-content > div > div.user-base.am-radius > div.user-base-left > div.items > a:nth-child(1) > span").click()
        time.sleep(2)
        element = self.driver.find_element(By.CSS_SELECTOR,"#user-avatar-popup > div > div.am-popup-bd > form > div.submit-operation.am-margin-top-xs > div > input")
        print("个人中心——修改头像——选择图片的元素是否可见:", element.is_displayed())  # 确认元素可见
        val = element.get_attribute('data-validation-message')
        #print(f"val={val}")
        assert val=="请选择需要上传的图片","属性错误"
        text = self.driver.find_element(By.CSS_SELECTOR, "#user-avatar-popup > div > div.am-popup-bd > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example").text
        assert text == "确认上传", "确认上传文本错误"

        #取消
        self.driver.find_element(By.CSS_SELECTOR,"#user-avatar-popup > div > div.am-popup-hd.meila-radius > span").click()
        number += 1

        #修改资料
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main.user-center > div.user-content > div > div.user-base.am-radius > div.user-base-left > div.items > a:nth-child(2) > span").click()

        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dt:nth-child(5)").text
        assert text == "地址", "地址文本错误"
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dd:nth-child(6) > span").text
        assert text == "未填写", "文本错误"
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dt:nth-child(7)").text
        assert text == "性别", "文本错误"
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dd:nth-child(8)").text
        assert text == "保密", "文本错误"
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dt:nth-child(11)").text
        assert text == "手机", "文本错误"
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dd:nth-child(12) > span.am-text-grey").text
        assert text == "未填写", "文本错误"
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dd:nth-child(12) > span.span-edit > a")
        assert element.text == "修改", "文本错误"
        element.click()
        #输入错误的手机号码模拟
        self.driver.find_element(By.CSS_SELECTOR, "#accounts").send_keys("1133")
        #获取验证码
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > form > div:nth-child(2) > div > span > button").click()
        #输入验证码
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > form > div:nth-child(2) > div > input").send_keys("1133")
        #点击确认
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > form > div:nth-child(3) > button").click()
        self.driver.back() #返回

        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dt:nth-child(13)").text
        assert text == "邮箱", "文本错误"
        text = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dd:nth-child(14) > span.am-text-grey").text
        assert text == "未填写", "文本错误"
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > dl > dd:nth-child(14) > span.span-edit > a")
        assert element.text == "修改", "文本错误"
        element.click()
        # 输入错误的邮箱号码模拟
        self.driver.find_element(By.CSS_SELECTOR, "#accounts").send_keys("1133")
        # 获取验证码
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > form > div:nth-child(2) > div > span > button").click()
        # 输入验证码
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > form > div:nth-child(2) > div > input").send_keys("1133")
        # 点击确认
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > form > div:nth-child(3) > button").click()
        self.driver.back()  # 返回
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > div > a > span")
        assert element.text == "编辑", "文本错误"
        element.click()
        #输入昵称
        element=self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > form > div.am-form-popup-content > div:nth-child(1) > input")
        element.clear()
        element.send_keys("dz")
        #点击保存
        self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > form > div.am-form-popup-submit > button").click()
        #修改后会自动返回个人资料界面
        #再点击返回个人中心
        self.driver.find_element(By.CSS_SELECTOR,"#user-offcanvas > div > ul > li:nth-child(1) > a").click()
        number += 1
        #点击我的地址
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main.user-center > div.user-content > div > div.user-base.am-radius > div.user-base-left > div.items > a:nth-child(3) > span").click()

        self.driver.fullscreen_window()
        # 点击新增
        self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > div.control > button").click()
        # 等待并切换到 iframe
        iframe = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.CSS_SELECTOR, "iframe[id^='common-popup-iframe-']"))
        )#id^指定id的前缀去定位元素，因为id的后缀是变化的，不能直接定位
        self.driver.switch_to.frame(iframe)
        # 在 iframe 内查找元素
        element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(1) > input"))
        )
         # 输入姓名
        element.clear()
        element.send_keys("dz")
        # 输入电话
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")
        element.clear()
        element.send_keys("111111")
        # 点击选择省市
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-suffix > div").click()
        # 点击河北省
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.province.am-cascader-menu > div > ul > li:nth-child(3) > span").click()
        # 点击石家庄市
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.city.am-cascader-menu.am-active > div > ul > li:nth-child(1) > span").click()

        # 点击长安区
        element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR,
            "li.am-cascader-node[data-name='长安区'] span.am-cascader-node-label"))
        )
        element.click()
        # 输入详细地址
        self.driver.find_element(By.CSS_SELECTOR, "#form-address").send_keys("1")
        #点击保存，显示错误因为电话格式错误
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example").click()
        #点击取消新增地址页面
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-warning.am-radius.am-btn-xs.am-margin-right-lg").click()
        # 从iframe 切回主页面
        self.driver.switch_to.default_content()
        self.driver.back()  # 返回个人中心
        #点击商城首页回到商城
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-home > div").click()


    except Exception as e:
        print("Handle_person failed")
        print(f"number={number}")
        print(e)
        # 添加截图
        ShoppingDriver.getScreeShot()
        raise

#首页商品全流程交互测试（核心业务）
def Handle_goods(self):
    #处理首页商品的交互
    try:
        # 模拟鼠标向下滑动,到数码办公区
        # 向下滚动650像素
        self.driver.execute_script("window.scrollBy(0, 650);")
        # 对数码办公区元素进行检查
        # 点击左示例图
        element = self.driver.find_element(By.CSS_SELECTOR,
        "#floor1 > div.am-g.am-container.floor.floor-flex > div.am-u-sm-3.aggregation-container > div > a > img")
        element.click()
        #窗口句柄跳转
        ShoppingHome.Switch_windows(self,True)
        #进入数码办公的商品分类区
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.breadcrumb-data.am-hide-sm-only > div > ul > li:nth-child(2) > div > span").text
        if text != "数码办公":
            print("商品分区显示错误")
        #获取数码商品区的总数
        nums = self.driver.find_element(By.CSS_SELECTOR, "#search-map > div > div > ul > li:nth-child(1) > p > strong").text
        if nums != "8":
            print("商品数量显示错误")

        # 点击筛选100以下价格的数码产品
        self.driver.find_element(By.CSS_SELECTOR,"#search-map > div > div > ul > li.am-nbfc.screening-price-container > div.map-right.map-text-items.am-fr.am-nbfc.theme-data-edit-event > ul > li:nth-child(1) > a").click()
        # 向下滚动200像素
        self.driver.execute_script("window.scrollBy(0, 200);")
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div:nth-child(6) > div > div.table-no.am-radius > p").text
        if text != "没有相关数据":
            print("商品无货没有提醒")

        # 再次100以下价格，取消价格限制，显示所有数码产品
        self.driver.find_element(By.CSS_SELECTOR,
        "#search-map > div > div > ul > li.am-nbfc.screening-price-container > div.map-right.map-text-items.am-fr.am-nbfc.theme-data-edit-event > ul > li:nth-child(1) > a").click()
        # 向下滚动500像素
        self.driver.execute_script("window.scrollBy(0, 500);")
        nums=int(nums)
        for i in range(1, nums+1):  # range(1,8+1) 表示 8张图
            #验证nums张图片都可见，数量要对上，多一个数量的视频就要报错
            selector=f"body > div.body-content-container > div.body-content-formal-container > div:nth-child(6) > div > div.module-goods-container.module-goods-grid-container.module-goods-grid-base > ul > li:nth-child({i}) > div > a > img"
            element = self.driver.find_element(By.CSS_SELECTOR, selector)
            assert element.is_displayed(), f"数码产品区第{i}张图不可见"

        #第nums+1张图不存在应该返回空列表
        element = self.driver.find_elements(By.CSS_SELECTOR, f"body > div.body-content-container > div.body-content-formal-container > div:nth-child(6) > div > div.module-goods-container.module-goods-grid-container.module-goods-grid-base > ul > li:nth-child({nums+1}) > div > a > img")
        if len(element) != 0:
            print("商品数量与声明数量不符")

        # 关闭新窗口，回到主窗口
        ShoppingHome.Switch_windows(self, False)
        # 循环获取第 1 到第 8 个元素
        for i in range(1, 9):  # range(1,9) 表示 8张图
            # 用 f-string 格式化字符串，将变量 i 嵌入选择器
            #拿到商品的售价和原价
            selector_now = f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > strong"
            element_now = self.driver.find_element(By.CSS_SELECTOR, selector_now)
            price_now_display = element_now.text # 首页显示的售价

            selector_prev = f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > span.original-price.am-text-truncate"
            element_prev = self.driver.find_elements(By.CSS_SELECTOR, selector_prev)
            price_prev_display = "-1" # 不打折的话就是没有原价
            # 一个商品可能是不打折的，所以element_prev可能不存在
            if len(element_prev) > 0:
                # 找到了元素，进行相应操作
                element = element_prev[0]
                price_prev_display=element.text # 首页显示的原价

            # 拿到商品名称的文本
            selector_text=f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.am-text-truncate-2-md.goods-title > a"
            element_text = self.driver.find_element(By.CSS_SELECTOR, selector_text)
            text_display = element_text.text
            # 商品售价：price_now_display 商品原价：price_prev_display 商品名称：text_display
            #print(f"商品售价：{price_now_display} 商品原价：{price_prev_display} 商品名称：{text_display}")
            #点击商品名称跳转到商品详情页
            self.driver.find_element(By.CSS_SELECTOR, selector_text).click()
            # 窗口句柄跳转
            ShoppingHome.Switch_windows(self, True)
            # 拿到商品的售价和原价
            selector_now = f"body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > div > div > div.tb-detail-price-content > div.items.goods-price.goods-sale-price > dd"
            element_now = self.driver.find_element(By.CSS_SELECTOR, selector_now)
            price_now_detail = element_now.text  # 详情页显示的售价

            selector_prev = f"body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > div > div > div.tb-detail-price-content > div.items.goods-price.goods-original-price > dd"
            element_prev = self.driver.find_elements(By.CSS_SELECTOR, selector_prev)
            price_prev_detail = "-1"  # 不打折的话就是没有原价
            # 一个商品可能是不打折的，所以element_prev可能不存在
            if len(element_prev) > 0:
                # 找到了元素，进行相应操作
                element = element_prev[0]
                price_prev_detail = element.text.replace(" ","") # 替换所有空格为空  # 详情页显示的原价

            # 收藏功能
            # 点击收藏之后，会出现弹窗收藏成功，捕捉收藏成功的弹窗的文本信息
            self.driver.find_element(By.CSS_SELECTOR, "#clearcontent > div.photo-bottom.am-cf > span").click()
            close_text = WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.XPATH, "/html/body/div[7]/div/p"))  # 提示成功的文本是否可见
            )
            text = close_text.text  # 收集文本信息
            if text != "收藏成功":
                print("提示词显示错误")
            close_btn = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/button"))  # 弹窗是否可点击关闭
            )
            close_btn.click()  # 点击关闭警告弹窗的按钮
            # 收藏成功后收藏的文本会更改为已收藏，验证文本
            # 看收藏夹里面有没有对应的商品
            # 点击我的收藏，再点击商品收藏
            self.driver.find_element(By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > a > span").click()
            element = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CSS_SELECTOR,
                "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > ul > li > a"))
            )  # 按钮是否可点击
            element.click()  # 点击商品收藏按钮，跳转到收藏夹页面

            # 获取收藏的商品列表
            element = self.driver.find_elements(
                By.CSS_SELECTOR,  # 原先的css是带可变数字的，所以只用后缀去查找元素
                'td.am-nowrap-initial.am-text-middle.am-grid-lg > div > a.am-nowrap-initial'
            )  # #data-list-5674 > td.am-nowrap-initial.am-text-middle.am-grid-lg > div > a.am-nowrap-initial
            for ele in element:  # 找出刚刚收藏的对应商品
                #print(ele.text)
                if ele.text != text_display:
                    print("收藏商品失败")
            # 回退到商品详情页
            self.driver.back()
            # 点击取消
            self.driver.find_element(By.CSS_SELECTOR, "#clearcontent > div.photo-bottom.am-cf > span").click()
            close_text = WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.XPATH, "/html/body/div[7]/div/p"))  # 取消成功的文本是否可见
            )
            text = close_text.text  # 收集文本信息
            if text != "取消成功":
                print("提示词显示错误")
            close_btn = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/button"))  # 弹窗是否可点击关闭
            )
            close_btn.click()  # 点击关闭警告弹窗的按钮
            # 点击我的收藏，再点击商品收藏
            self.driver.find_element(By.CSS_SELECTOR,
                                     "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > a > span").click()
            element = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CSS_SELECTOR,
                                            "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > ul > li > a"))
            )  # 按钮是否可点击
            element.click()  # 点击商品收藏按钮，跳转到收藏夹页面

            # 获取收藏的商品列表
            element = self.driver.find_elements(
                By.CSS_SELECTOR,  # 原先的css是带可变数字的，所以只用后缀去查找元素
                'td.am-nowrap-initial.am-text-middle.am-grid-lg > div > a.am-nowrap-initial'
            )  # #data-list-5674 > td.am-nowrap-initial.am-text-middle.am-grid-lg > div > a.am-nowrap-initial
            for ele in element:  # 找刚刚取消的对应商品，看还在不在
                #print(ele.text)
                if ele.text == text_display:
                    print("取消收藏功能失败")
            # 回退到商品详情页
            self.driver.back()

            # 在详情页的购买功能点击之前，如果有套餐，要选择套餐、颜色、容量
            package="body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.sku-container > div:nth-child(1) > ul > li > span"
            color="body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.sku-container > div:nth-child(2) > ul > li:nth-child(1) > span"
            capacity="body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.sku-container > div:nth-child(3) > ul > li:nth-child(1) > span"
            self.driver.fullscreen_window() #开全屏，不然套餐、颜色配置出不来
            time.sleep(1)
            element = self.driver.find_elements(By.CSS_SELECTOR,package)
            if len(element) > 0:
                ele = element[0]
                ele.click()
                time.sleep(2)
                #等待颜色按钮可点击
                element = WebDriverWait(self.driver,10).until(
                    EC.element_to_be_clickable((By.CSS_SELECTOR, color))
                )
                element.click()
                time.sleep(2)
                #等待容量按钮可点击
                element = WebDriverWait(self.driver, 10).until(
                    EC.element_to_be_clickable((By.CSS_SELECTOR, capacity))
                )
                element.click()
            # 拿到商品名称的文本
            selector_text = f"body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-hd.am-margin-bottom-xs > h1"
            element_text = self.driver.find_element(By.CSS_SELECTOR, selector_text)
            text_detail = element_text.text
            #如果商品详情页价格会浮动，只取-前的最小值去比较
            if '-' in price_now_detail:
                price_now_detail = price_now_detail.split('-')[0]
            if '-' in price_prev_detail:
                price_prev_detail = price_prev_detail.split('-')[0]

            if price_now_display != price_now_detail:
                print(f"首页数码区第{i}张图商品售价和详情页售价不同，首页售价：{price_now_display}，详情页售价：{price_now_detail}")
            if price_prev_display != price_prev_detail:
                print(f"首页数码区第{i}张图商品原价和详情页原价不同，首页原价：{price_prev_display}，详情页原价：{price_prev_detail}")
            if text_display != text_detail:
                print(f"首页数码区商品名称和详情页名称不同，首页名称：{text_display}，详情页名称：{text_detail}")

            # 读取商品数量
            count=self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.tb-detail-list > dl > dd > div.theme-popover > div.theme-popbod.dform.am-background-white > form > div.theme-signin-left > div.theme-options.am-flex.am-flex-row.am-flex-items-center > dd > div > input")
            price_count=count.get_attribute("value")
            price_count=float(price_count)

            # 购物车功能
            # 点击加入购物车
            self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-secondary.cart-submit.buy-event.login-event").click()
            # 出现弹窗，选择去购物车结算或继续购物
            # 选择继续购物，手动再打开购物车
            close_text = WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR, "#common-user-cart-success-modal > div > div.am-modal-bd > div.am-text-center.am-margin-top > span"))
            ) # 商品成功加入购物车的文本是否可见
            text = close_text.text  # 收集文本信息
            text= text.replace(' ','')
            if text != "商品已成功加入购物车！":
                print(f"提示词显示错误，提示词为{text}")
            close_shopping = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CSS_SELECTOR, "#common-user-cart-success-modal > div > div.am-modal-bd > div:nth-child(3) > button"))
            )# 继续购物按钮是否可点击
            close_shopping.click()  # 点击继续购物
            # 点击购物车，跳转到购物车
            self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > span").click()
            element = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CSS_SELECTOR,"td.base > label > span" ))
            ) # 等待购物车的勾选按钮加载好，可以点击时
            # 勾选到要购买的商品
            element = self.driver.find_elements(By.CSS_SELECTOR, "td.base > label > span")
            if len(element) == 0:
                print("购物车添加失败")
            for ele in element:
                ele.click() # 把购物车里所有物品勾选
            # 点击结算
            self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-right.am-fr.am-padding-right > form > button").click()
            # 向下滚动700像素
            self.driver.execute_script("window.scrollBy(0, 700);")
            # 点击提交订单
            self.driver.find_element(By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div:nth-child(5) > div > div.am-background-white.am-radius.am-padding-bottom-main > div.buy-nav-footer.am-padding-horizontal-main > div > form > div > button").click()
            self.driver.back() # 回退到购物车
            element = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CSS_SELECTOR, "td.operation > div > a.am-color-grey-dark.submit-delete"))
            )  # 等待购物车的删除按钮加载好，可以点击时
            # 点击删除按钮
            element = self.driver.find_elements(By.CSS_SELECTOR, "td.operation > div > a.am-color-grey-dark.submit-delete")
            if len(element) == 0:
                print("购物车删除失败")
            for ele in element:
                ele.click()  # 把购物车里所有物品删除
                # 点击确认删除
                WebDriverWait(self.driver, 10).until(
                    EC.element_to_be_clickable(
                        (By.CSS_SELECTOR, "div > div.am-modal-footer > span:nth-child(2)"))
                )  # 等待删除提示框
                self.driver.find_element(By.CSS_SELECTOR,"div > div.am-modal-footer > span:nth-child(2)").click()

            self.driver.back()  # 回退到商品详情页

            self.driver.fullscreen_window()  # 开全屏，不然套餐、颜色配置出不来
            time.sleep(1)
            element = self.driver.find_elements(By.CSS_SELECTOR, package)
            if len(element) > 0:
                ele = element[0]
                ele.click()
                time.sleep(2)
                # 等待颜色按钮可点击
                element = WebDriverWait(self.driver, 10).until(
                    EC.element_to_be_clickable((By.CSS_SELECTOR, color))
                )
                element.click()
                time.sleep(2)
                # 等待容量按钮可点击
                element = WebDriverWait(self.driver, 10).until(
                    EC.element_to_be_clickable((By.CSS_SELECTOR, capacity))
                )
                element.click()

            #在详情页的购买功能
            # 点击购买
            self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.system-goods-detail > div.item-inform.am-container.am-radius.theme-data-edit-event > div.clearfix-right > div > div.buy-nav > div.buy-submit-container.buy-submit-container-number-2 > button.am-radius.am-btn.am-btn-primary.buy-submit.buy-event.login-event").click()
            # 三种购买方式：快递、同城、自提
            # 向下滚动700像素
            self.driver.execute_script("window.scrollBy(0, 700);")
            # 快递
            # 确定实付款金额和商品显示售价一致
            text=self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div:nth-child(5) > div > div.am-background-white.am-radius.am-padding-bottom-main > div.buy-nav-footer.am-padding-horizontal-main > div > div > div.base-real-pay > span.price").text
            text = text.replace(" ", "").replace("￥","")  # 替换所有空格为空
            price_now_display = price_now_display.replace(" ", "").replace("￥", "")
            text = float(text)
            price_now_display = float(price_now_display)
            expected = price_now_display * price_count
            if abs(text-expected) > 0.01: # float相乘时精度会偏差，无法直接比较
                print(f"支付价格和表明的售价不一致,支付价格为{text}，售价为：{expected}")
            # 点击提交订单
            self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div:nth-child(5) > div > div.am-background-white.am-radius.am-padding-bottom-main > div.buy-nav-footer.am-padding-horizontal-main > div > form > div > button").click()
            # 向上滚动700像素
            self.driver.execute_script("window.scrollBy(0, -700);")
            # 点击同城
            self.driver.find_element(By.CSS_SELECTOR, "#buy-items-tabs > ul > li:nth-child(2) > a").click()
            # 向下滚动700像素
            self.driver.execute_script("window.scrollBy(0, 700);")
            # 确定实付款金额和商品显示售价一致
            text = self.driver.find_element(By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div:nth-child(5) > div > div.am-background-white.am-radius.am-padding-bottom-main > div.buy-nav-footer.am-padding-horizontal-main > div > div > div.base-real-pay > span.price").text
            text = text.replace(" ", "").replace("￥","")  # 替换所有空格为空
            text = float(text) # price_now_display在上面已经转换好了
            expected = price_now_display * price_count
            if abs(text - expected) > 0.01:  # float相乘时精度会偏差，无法直接比较
                print(f"支付价格和表明的售价不一致,支付价格为{text}，售价为：{expected}")
            # 点击提交订单
            self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div:nth-child(5) > div > div.am-background-white.am-radius.am-padding-bottom-main > div.buy-nav-footer.am-padding-horizontal-main > div > form > div > button").click()
            #print(f"第{i}张图等待关闭弹窗")
            close_btn = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.XPATH,"/html/body/div[6]/button"))
            )
            close_btn.click() # 点击关闭警告弹窗的按钮
            #print("关闭弹窗")
            # 向上滚动700像素
            self.driver.execute_script("window.scrollBy(0, -700);")
            # 点击自提
            self.driver.find_element_by_xpath('//*[@id="buy-items-tabs"]/ul/li[3]/a').click()
            # 点击选择取货地址
            self.driver.find_element(By.CSS_SELECTOR, "#buy-items-address > div.am-text-center > button").click()
            time.sleep(1)
            # 点击总部
            self.driver.find_element(By.CSS_SELECTOR, "#extraction-address-popup > div > div.am-popup-bd > ul > li:nth-child(1) > div > button").click()
            # 向下滚动700像素
            self.driver.execute_script("window.scrollBy(0, 700);")
            # 确定实付款金额和商品显示售价一致
            text = self.driver.find_element(By.CSS_SELECTOR,
            "body > div.body-content-container > div.body-content-formal-container > div:nth-child(5) > div > div.am-background-white.am-radius.am-padding-bottom-main > div.buy-nav-footer.am-padding-horizontal-main > div > div > div.base-real-pay > span.price").text
            text = text.replace(" ", "").replace("￥", "")  # 替换所有空格为空
            text = float(text)  # price_now_display在上面已经转换好了
            expected = price_now_display * price_count
            if abs(text - expected) > 0.01:  # float相乘时精度会偏差，无法直接比较
                print(f"支付价格和表明的售价不一致,支付价格为{text}，售价为：{expected}")
            # 点击提交订单
            self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div:nth-child(5) > div > div.am-background-white.am-radius.am-padding-bottom-main > div.buy-nav-footer.am-padding-horizontal-main > div > form > div > button").click()

            # 关闭新窗口，回到主窗口
            ShoppingHome.Switch_windows(self, False)
    except Exception as e:
        print("Handle_goods failed")
        print(e)
        # 添加截图
        ShoppingDriver.getScreeShot()
        raise

